package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value="APP用户信息",tags={"APP用户信息"})
@RequestMapping("app/user")
@RestController
public class AppUserController extends AppBaseController{

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private SysPasswordService passwordService;


    @ApiOperation(value = "获取用户信息")
    @ApiImplicitParam(name = "X-TaiChi-Token", value = "apyexnioxzsa11cqxy2ur2p9jkp46hz7",
            paramType = "header", dataType = "string", required = true)
    @GetMapping("/info")
    public AjaxResult user(){
        SysUser appUser = iSysUserService.selectUserById(getUserId());
        UserVO userVO = new UserVO();
        if(null!=appUser){
            userVO.setId(appUser.getDeptId());
            userVO.setAvatar(appUser.getAvatar());
            userVO.setDeptName(appUser.getDept().getDeptName());
            userVO.setDeptCode(appUser.getDept().getDeptCode());
            userVO.setLoginName(appUser.getLoginName());
            userVO.setName(appUser.getUserName());
            userVO.setIdCard(appUser.getIdCard());
            userVO.setPhonenumber(appUser.getPhonenumber());
            if(null!=appUser.getSex()&&appUser.getSex()=="0"){
                userVO.setSex("女");
            }else{
                userVO.setSex("男");
            }
        }
        return AjaxResult.success("用户获取成功！",userVO);
    }

    @PostMapping("/reset-password")
    @ApiOperation(value = "用户重置密码",response=AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-TaiChi-Token", value = "apyexnioxzsa11cqxy2ur2p9jkp46hz7",
                    paramType = "header", dataType = "string", required = true),
            @ApiImplicitParam(name="oldPassword",value="旧密码",required=true ),
            @ApiImplicitParam(name="newPassword",value="新密码",required=true)
    })
    public AjaxResult resetPassword(@RequestParam String oldPassword ,@RequestParam String newPassword){

        SysUser appUser = iSysUserService.selectUserById(getUserId());
        if(null==appUser){
            return  AjaxResult.error("数据异常");
        }

        String password = passwordService.encryptPassword(appUser.getLoginName(), oldPassword, appUser.getSalt());
        if(!appUser.getPassword().equals(password)){
            return  AjaxResult.error("旧密码不正确");
        }

        //更新密码
        String newPass = passwordService.encryptPassword(appUser.getLoginName(), newPassword, appUser.getSalt());
        appUser.setPassword(newPass);
        iSysUserService.updateUser(appUser);
        return AjaxResult.success("密码更新成功");

    }


}
